/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hxtec.polaris.jobs;

import java.text.SimpleDateFormat;
import java.util.*;

import com.hxtec.polaris.entity.ShopProductBase;
import com.hxtec.polaris.entity.ShopProductVariant;
import com.hxtec.polaris.entity.ShopStock;
import com.hxtec.polaris.mapper.ShopProductBaseMapper;
import com.hxtec.polaris.mapper.ShopProductVariantMapper;
import com.hxtec.polaris.mapper.ShopStockMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ScheduledTasks {

    @Resource
    private ShopProductBaseMapper baseMapper;
    @Resource
    private ShopProductVariantMapper variantMapper;
    @Resource
    private ShopStockMapper stockMapper;

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);


    @Scheduled(cron = "0 0 0 ? * ? ")
    public void updateWeight() {
        log.info("UpdateWeight task start");
        List<ShopStock> shopStocks = stockMapper.selectAll();
        List<ShopProductVariant> shopProductVariants = variantMapper.selectAll();
        List<ShopProductBase> shopProductBases = baseMapper.selectAll();

        Map<Integer, DTO> skus = new HashMap<>();
        Map<Integer, DTO> spus = new HashMap<>();

        shopStocks.forEach(i -> {
            skus.put(i.getSku(), new DTO(0, i.getSku(), 0, i.getLock()));
        });
        shopProductVariants.forEach(i -> {
            DTO dto = skus.get(i.getSku());
            if (dto != null) {
                dto.spu = i.getSpu();
                dto.price = i.getPrice().intValue();
                skus.put(i.getSku(), dto);
            } else {
                skus.put(i.getSku(), new DTO(i.getSpu(), i.getSku(), i.getPrice().intValue(), 0));
            }
        });
        shopProductBases.forEach(i -> {
            spus.put(i.getSpu(), new DTO(i.getSpu(), 0, 0, 0));
        });
        skus.forEach((k, v) -> {
            DTO dto = spus.get(v.spu);
            if (dto != null) {
                dto.price += v.price;
                dto.sales += v.sales;
                spus.put(v.spu, dto);
            }
        });

        List<DTO> skulist = new ArrayList<>(skus.values());
        skulist.sort((a, b) -> {
            Double wa = 0.8 * a.sales + 0.2 * a.price;
            Double wb = 0.8 * b.sales + 0.2 * b.price;
            return wa.compareTo(wb);
        });
        for (int i = 0; i < skulist.size(); i++) {
            ShopProductVariant shopProductVariant = variantMapper.selectByPrimaryKey(skulist.get(i).sku);
            shopProductVariant.setWeight((double) i);
            variantMapper.updateByPrimaryKeySelective(shopProductVariant);
        }


        List<DTO> spulist = new ArrayList(spus.values());
        spulist.sort((a, b) -> {
            Double wa = 0.8 * a.sales + 0.2 * a.price;
            Double wb = 0.8 * b.sales + 0.2 * b.price;
            return wa.compareTo(wb);
        });
        for (int i = 0; i < spulist.size(); i++) {
            ShopProductBase shopProductBase = baseMapper.selectByPrimaryKey(spulist.get(i).spu);
            shopProductBase.setWeight((double) i);
            baseMapper.updateByPrimaryKeySelective(shopProductBase);
        }
    }


    static class DTO {
        int spu;
        int sku;
        int price;
        int sales;

        public DTO(int spu, int sku, int price, int sales) {
            this.spu = spu;
            this.sku = sku;
            this.price = price;
            this.sales = sales;
        }

        @Override
        public String toString() {
            return "DTO{" +
                    "spu=" + spu +
                    ", sku=" + sku +
                    ", price=" + price +
                    ", sales=" + sales +
                    '}';
        }
    }

}